package com.kuzlak.cms.model;

import com.kuzlak.cms.config.ConstantHeader;
import com.kuzlak.cms.module.sys.entity.SysLoginLog;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.util.Date;

public class PermissionSqlModel {



    public static String dataPer() {
        String sql = "ORGAN_ID IN(SELECT ID FROM organ)";

        SysLoginLog adminToken = ConstantHeader.getAdminToken();
        int managementArea = adminToken.getManagementArea();
        long userId = adminToken.getUserId();
        Long organId = adminToken.getOrganId();
        Integer organType = adminToken.getOrganType();
        switch (organType) {
            case 1:
                String perIds = adminToken.getPerIds();
                if (StringUtils.isNotEmpty(perIds)) {
                    sql = "DEPT_ID IN(" + perIds + ")";
                } else {

                    sql = "DEPT_ID IN(SELECT ID FROM sys_departments as tdepartments WHERE  ID IN (SELECT  DEPT_ID FROM sys_role_dept as rd  WHERE TYPE=0 and ROLE_ID in(SELECT ROLE_ID FROM sys_user_role as ur  WHERE ur.USER_ID=" + userId + ")) UNION SELECT ID FROM sys_departments as tdepartments WHERE ORGAN_ID   IN (SELECT  DEPT_ID FROM sys_role_dept as rd  WHERE TYPE=1 and ROLE_ID in(SELECT ROLE_ID FROM sys_user_role as ur  WHERE ur.USER_ID=" + userId + "))";
                }
                break;
            case 2:
                sql = "ORGAN_ID IN(SELECT ID FROM  sys_organ as tsys_organ   WHERE   DISPOSE_ORGAN_ID=" + organId + ")";
                break;
            case 3:
                sql = "ORGAN_ID IN(SELECT ID FROM sys_organ as tsys_organ where ADDRESS_CODE like '" + managementArea + "%')";
                break;
        }
        // String sql="SELECT DEPT_ID FROM sys_role_dept  WHERE  ROLE_ID in(SELECT ROLE_ID FROM sys_user_role as ur WHERE ur.USER_ID="+userId+")";

        //   SELECT ID FROM sys_departments as tdepartments WHERE  ID IN (SELECT  DEPT_ID FROM sys_role_dept as rd  WHERE TYPE=0 and ROLE_ID in(SELECT ROLE_ID FROM sys_user_role as ur  WHERE ur.USER_ID=#{userId})) UNION SELECT ID FROM sys_departments as tdepartments WHERE ORGAN_ID   IN (SELECT  DEPT_ID FROM sys_role_dept as rd  WHERE TYPE=1 and ROLE_ID in(SELECT ROLE_ID FROM sys_user_role as ur  WHERE ur.USER_ID=#{userId}))

        return sql;
    }

    public static String dataOarganPer() {
        String sql = "ORGAN_ID IN(SELECT ID FROM organ)";

        SysLoginLog adminToken = ConstantHeader.getAdminToken();
        int managementArea = adminToken.getManagementArea();
        long userId = adminToken.getUserId();
        Long organId = adminToken.getOrganId();
        Integer organType = adminToken.getOrganType();
        switch (organType) {
            case 1:
                String perIds = adminToken.getPerIds();
                if (StringUtils.isNotEmpty(perIds)) {
                    sql = "ID IN(" + perIds + ")";
                } else {

                    sql = "ID IN(SELECT ID FROM sys_departments as tdepartments WHERE  ID IN (SELECT  DEPT_ID FROM sys_role_dept as rd  WHERE TYPE=0 and ROLE_ID in(SELECT ROLE_ID FROM sys_user_role as ur  WHERE ur.USER_ID=" + userId + ")) UNION SELECT ID FROM sys_departments as tdepartments WHERE ORGAN_ID   IN (SELECT  DEPT_ID FROM sys_role_dept as rd  WHERE TYPE=1 and ROLE_ID in(SELECT ROLE_ID FROM sys_user_role as ur  WHERE ur.USER_ID=" + userId + "))";
                }
                sql = "ORGAN_ID IN(SELECT ORGAN_ID FROM sys_departments as tdepartments  WHERE " + sql + ")";
                break;
            case 2:
                sql = "ORGAN_ID IN(SELECT ID FROM  sys_organ as tsys_organ   WHERE   DISPOSE_ORGAN_ID=" + organId + ")";
                break;
            case 3:
                sql = "ORGAN_ID IN(SELECT ID FROM  sys_organ as tsys_organ  where ADDRESS_CODE like '" + managementArea + "%')";
                break;
        }
        // String sql="SELECT DEPT_ID FROM sys_role_dept  WHERE  ROLE_ID in(SELECT ROLE_ID FROM sys_user_role as ur WHERE ur.USER_ID="+userId+")";

        //   SELECT ID FROM sys_departments as tdepartments WHERE  ID IN (SELECT  DEPT_ID FROM sys_role_dept as rd  WHERE TYPE=0 and ROLE_ID in(SELECT ROLE_ID FROM sys_user_role as ur  WHERE ur.USER_ID=#{userId})) UNION SELECT ID FROM sys_departments as tdepartments WHERE ORGAN_ID   IN (SELECT  DEPT_ID FROM sys_role_dept as rd  WHERE TYPE=1 and ROLE_ID in(SELECT ROLE_ID FROM sys_user_role as ur  WHERE ur.USER_ID=#{userId}))

        return sql;
    }

    public static String orgenTypeIds(int type) {
        return "(SELECT ID FROM  sys_organ as tsys_organ   WHERE   TYPE=" + type + ")";
    }

    public static String dataDeptPer() {
        String sql = "ID IN(SELECT ID FROM sys_departments)";

        SysLoginLog adminToken = ConstantHeader.getAdminToken();
        int managementArea = adminToken.getManagementArea();
        long userId = adminToken.getUserId();
        Long organId = adminToken.getOrganId();
        Integer organType = adminToken.getOrganType();
        switch (organType) {
            case 1:
                String perIds = adminToken.getPerIds();
                if (StringUtils.isNotEmpty(perIds)) {
                    sql = "ID IN(" + perIds + ")";
                } else {

                    sql = "ID IN(SELECT ID FROM sys_departments WHERE  ID IN (SELECT  DEPT_ID FROM sys_role_dept as rd  WHERE TYPE=0 and ROLE_ID in(SELECT ROLE_ID FROM sys_user_role as ur  WHERE ur.USER_ID=" + userId + ")) UNION SELECT ID FROM sys_departments as tdepartments WHERE ORGAN_ID   IN (SELECT  DEPT_ID FROM sys_role_dept as rd  WHERE TYPE=1 and ROLE_ID in(SELECT ROLE_ID FROM sys_user_role as ur  WHERE ur.USER_ID=" + userId + "))";
                }
                break;
            case 2:
                sql = "ORGAN_ID IN(SELECT ID FROM  sys_organ as tsys_organ   WHERE   DISPOSE_ORGAN_ID=" + organId + ")";
                break;
            case 3:
                sql = "ORGAN_ID IN(SELECT ID FROM  sys_organ as tsys_organ  where ADDRESS_CODE like '" + managementArea + "%')";
                break;
        }
        // String sql="SELECT DEPT_ID FROM sys_role_dept  WHERE  ROLE_ID in(SELECT ROLE_ID FROM sys_user_role as ur WHERE ur.USER_ID="+userId+")";

        //   SELECT ID FROM sys_departments as tdepartments WHERE  ID IN (SELECT  DEPT_ID FROM sys_role_dept as rd  WHERE TYPE=0 and ROLE_ID in(SELECT ROLE_ID FROM sys_user_role as ur  WHERE ur.USER_ID=#{userId})) UNION SELECT ID FROM sys_departments as tdepartments WHERE ORGAN_ID   IN (SELECT  DEPT_ID FROM sys_role_dept as rd  WHERE TYPE=1 and ROLE_ID in(SELECT ROLE_ID FROM sys_user_role as ur  WHERE ur.USER_ID=#{userId}))

        return sql;
    }

    public static String dataOrganPer() {
        String sql = "ID IN(SELECT ID FROM sys_organ)";

        SysLoginLog adminToken = ConstantHeader.getAdminToken();
        int managementArea = adminToken.getManagementArea();
        long userId = adminToken.getUserId();
        Long organId = adminToken.getOrganId();
        Integer organType = adminToken.getOrganType();
        switch (organType) {
            case 1:

                sql = "ID=" + adminToken.getOrganId();

                break;
            case 2:
                sql = "ID IN(SELECT ID FROM  sys_organ as tsys_organ   WHERE   DISPOSE_ORGAN_ID=" + organId + ")";
                break;
            case 3:
                sql = " ADDRESS_CODE like '" + managementArea + "%'";
                break;
        }
        // String sql="SELECT DEPT_ID FROM sys_role_dept  WHERE  ROLE_ID in(SELECT ROLE_ID FROM sys_user_role as ur WHERE ur.USER_ID="+userId+")";

        //   SELECT ID FROM sys_departments as tdepartments WHERE  ID IN (SELECT  DEPT_ID FROM sys_role_dept as rd  WHERE TYPE=0 and ROLE_ID in(SELECT ROLE_ID FROM sys_user_role as ur  WHERE ur.USER_ID=#{userId})) UNION SELECT ID FROM sys_departments as tdepartments WHERE ORGAN_ID   IN (SELECT  DEPT_ID FROM sys_role_dept as rd  WHERE TYPE=1 and ROLE_ID in(SELECT ROLE_ID FROM sys_user_role as ur  WHERE ur.USER_ID=#{userId}))

        return sql;
    }

    public static String menus(long userId) {
        String sql = "SELECT MENU_ID FROM role_menu  WHERE  ROLE_ID in(SELECT ROLE_ID FROM sys_user_role as ur WHERE ur.USER_ID=" + userId + ")";
        return sql;
    }

    public static String serachName(String name) {
        String sql = "SELECT ID FROM sys_menu WHERE ID IN(SELECT  ID FROM sys_menu where NAME LIKE '%" + name + "%' UNION SELECT  PARENT_ID FROM sys_menu where NAME LIKE '%" + name + "%' UNION SELECT  (SELECT PARENT_ID FROM  sys_menu WHERE ID=m.PARENT_ID) FROM sys_menu as m where NAME LIKE '%" + name + "%')";
        return sql;

    }

    public static String goodsPlateCar(String carPlateNum) {
        String sql = "SELECT ID FROM car  WHERE   PLATE_NUMBER='" + carPlateNum + "'";
        return sql;
    }

    public static String goodsQrCode(String qrCode) {
        String sql = "SELECT ID FROM goods  WHERE   QR_CODE='" + qrCode + "'";
        return sql;
    }

    public static String eqUserName(String username) {
        String sql = "SELECT ID FROM users  WHERE   USER_NAME=" + username + "";
        return sql;
    }

    public static String eqUserNameR(String username) {
        String sql = "SELECT ID FROM users  WHERE   NAME=" + username + "";
        return sql;
    }

    public static String userNameEq(String username) {
        String sql = "SELECT ID FROM users  WHERE   USER_NAME LIKE '" + username + "%'";
        return sql;
    }

    public static String likeUserName(String username) {
        String sql = "SELECT ID FROM users  WHERE   NAME LIKE '" + username + "%'";
        return sql;
    }
    //  SELECT ID FROM users where  NAME LIKE '古%'

    public static String likeUserPhone(String phone) {
        String sql = "SELECT ID FROM users  WHERE   PHONE LIKE '" + phone + "%'";
        return sql;
    }

    public static String getUserOrganId(long userId) {
        String sql = "SELECT ORGAN_ID FROM users  WHERE   ID=" + userId + "";
        return sql;
    }

    public static String getRoleUserId(long roleId) {
        String sql = "SELECT USER_ID FROM sys_user_role  WHERE   ROLE_ID=" + roleId + "";
        return sql;
    }

    public static String likeOrganName(String organname) {
        String sql = "SELECT ID FROM  sys_organ as tsys_organ   WHERE   ORGAN_NAME LIKE '" + organname + "%'";
        return sql;
    }

    public static String diposeOrganId(long id) {
        String sql = "SELECT ID FROM  sys_organ as tsys_organ   WHERE   DISPOSE_ORGAN_ID=" + id;
        return sql;
    }


    public static String dateEqWhere(String key, String date) {

        String format = "%Y-%m-%d";
        String[] split = date.split("-");
        if (split.length == 1) {
            format = "%Y";
        }
        if (split.length == 2) {
            format = "%Y-%m";
        }
        String sql = "DATE_FORMAT(" + key + ",'" + format + "')='" + date + "'";
        return sql;

    }

    public static String dateStateWhere(String key, int type) {
        String formatDate = "";
        String formatSql = "%Y";
        switch (type) {
            case 1:
                formatSql = "%Y";
                Date date1 = new Date();
                formatDate = DateFormatUtils.format(date1, "yyyy");
                break;
            case 2:
                formatSql = "%Y-%m";
                Date date2 = new Date();
                formatDate = DateFormatUtils.format(date2, "yyyy-MM");
                break;
            case 3:
                formatSql = "%Y-%m-%d";
                Date date3 = new Date();
                formatDate = DateFormatUtils.format(date3, "yyyy-MM-dd");
                break;
            default:
                formatSql = "%Y";
                Date date4 = new Date();
                formatDate = DateFormatUtils.format(date4, "yyyy");
                break;
        }
        String sql = "DATE_FORMAT(" + key + ",'" + formatSql + "')='" + formatDate + "'";
        return sql;

    }


    public static String dateStateGeWhere(String key, int type) {
        String formatDate = "";
        String formatSql = "%Y";
        switch (type) {
            case 1:
                formatSql = "%Y";
                Date date1 = new Date();
                formatDate = DateFormatUtils.format(date1, "yyyy");
                break;
            case 2:
                formatSql = "%Y-%m";
                Date date2 = new Date();
                formatDate = DateFormatUtils.format(date2, "yyyy-MM");
                break;
            case 3:
                formatSql = "%Y-%m-%d";
                Date date3 = new Date();
                formatDate = DateFormatUtils.format(date3, "yyyy-MM-dd");
                break;
            default:
                formatSql = "%Y";
                Date date4 = new Date();
                formatDate = DateFormatUtils.format(date4, "yyyy");
                break;
        }
        String sql = "DATE_FORMAT(" + key + ",'" + formatSql + "')>='" + formatDate + "'";
        return sql;

    }

    public static String dateStateLtWhere(String key, int type) {
        String formatDate = "";
        String formatSql = "%Y";
        switch (type) {
            case 1:
                formatSql = "%Y";
                Date date1 = new Date();
                formatDate = DateFormatUtils.format(date1, "yyyy");
                break;
            case 2:
                formatSql = "%Y-%m";
                Date date2 = new Date();
                formatDate = DateFormatUtils.format(date2, "yyyy-MM");
                break;
            case 3:
                formatSql = "%Y-%m-%d";
                Date date3 = new Date();
                formatDate = DateFormatUtils.format(date3, "yyyy-MM-dd");
                break;
            default:
                formatSql = "%Y";
                Date date4 = new Date();
                formatDate = DateFormatUtils.format(date4, "yyyy");
                break;
        }
        String sql = "DATE_FORMAT(" + key + ",'" + formatSql + "')<'" + formatDate + "'";
        return sql;

    }


    public static String noticeUsers() {
        SysLoginLog adminToken = ConstantHeader.getAdminToken();
        Long userId = adminToken.getUserId();
        String sql = "SELECT NOTICE_ID FROM notice_user  WHERE   USER_ID=" + userId;
        return sql;
    }

    public static String findInSet(String key, String value) {

        String sql = "FIND_IN_SET('" + value + "'," + key + ")";
        return sql;
    }

    public static String likeCustomerName(String c) {
        return "SELECT ID FROM customer  WHERE   NAME LIKE '" + c + "%'";
    }


    public static String getOrganType(String organType) {

        return "SELECT ID FROM sys_organ where FIND_IN_SET(TYPE,'" + organType + "')";
    }
}
